﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class PhongDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" SELECT *,b.TENKHOA FROM phong a join khoa b on a.makhoa=b.makhoa";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM phong WHERE sophong = '{0}'";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool deleteobj(object obj)
        {
            var o = (phong)obj;
            string sql = @"delete from phong where sophong={0} and makhoa='{1}'";
            sql =string.Format(sql,o.sophong,o.makhoa);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (phong)obj;
            string sql = @"INSERT INTO phong(sophong,makhoa,tenphong) VALUES({0},'{1}',N'{2}')";
            sql = string.Format(sql, o.sophong, o.makhoa, o.tenphong);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (phong)obj;
            string sql = @"UPDATE phong SET tenphong=N'{0}'  where makhoa='{1}' and sophong={2}";
            sql = string.Format(sql,o.tenphong,o.makhoa,o.sophong);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_khoa()
        {
            string sql = @" SELECT makhoa as id, tenkhoa as ten FROM khoa";
            return ExecuteQuery(sql);
        }
        public string  Select_max(string makhoa) // dau tien lay ra cai ma cao nhat.
        {
            string sql = @"SELECT max(sophong) as id FROM phong where makhoa='{0}'";
            sql = string.Format(sql, makhoa);
            DataTable dt = ExecuteQuery(sql);
            return dt.Rows.Count > 0 ? "0"+dt.Rows[0]["id"] : "0";
            
        }

    }
}
